package com.example.demo.filter;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * fileName:MyFilter
 * description:
 * author: LJV
 * createTime:2022/4/9 13:37
 * version:1.0.0
 */
@Slf4j
@WebFilter
public class MyFilter implements Filter {

    private static String privateKey ;
    public String getPrivateKey() {
        return privateKey;
    }
    public void setPrivateKey(String key) {
        privateKey = key;
    }

    /**
     *  排除过滤路径
     */
    List<String> ignore = Arrays.asList("/xxxx");

    /**
     *   前缀排除   如 /static/goods 排除
     */
    List<String> ignorePrefix = Arrays.asList( "/css/", "/pop/", "/js/", "/static/", "/images/", "/favicon.ico");

    /**
     *  排除过滤路径
     */
    List<String> ignoreSuffix = Arrays.asList("/test");

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //过滤器初始化
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 防止流读取一次后就没有了, 所以需要将流继续写出去
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getServletPath();
        response.setContentType("application/json;charset=UTF-8");
        ServletRequest requestWrapper = null;
        if(canIgnore(uri)) {
            requestWrapper = new MyFilterBodyReaderHttpServletRequestWrapper(request);
            filterChain.doFilter(requestWrapper, response);
            return;
        }
        try {
            requestWrapper = new MyFilterBodyReaderHttpServletRequestWrapper(request, response, privateKey,true);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        filterChain.doFilter(requestWrapper, servletResponse);
    }

    @Override
    public void destroy() {
        //过滤器销毁
    }

    private boolean canIgnore(String uri) {

        log.info("过滤器  request  uri : {} ",uri);
        boolean isExcludedPage = false;
        for (String page : ignore) {
            if (uri.equals(page)) {
                log.info("请求路径不需要拦截，忽略该uri : {} ",uri);
                isExcludedPage = true;
                break;
            }
        }

        for (String prefix : ignorePrefix) {
            if (uri.startsWith(prefix)) {
                log.info("请求路径前缀[{}]，不拦截该uri : {} ", prefix, uri);
                isExcludedPage = true;
                break;
            }
        }

        for (String prefix : ignoreSuffix) {
            if (uri.endsWith(prefix)) {
                log.info("请求路径后缀[{}]，不拦截该uri : {} ", prefix, uri);
                isExcludedPage = true;
                break;
            }
        }
        return isExcludedPage;
    }

}
